import { getUser } from '../services/user';

export default {
  namespace: 'user',

  state: {
    customerName: '',
    contactTel: 0,
    isverify: 0,
    orderData: {},
  },

  effects: {
    *getUserList({ payload }, { call, put }) {
      const response = yield call(getUser, payload);

      yield put({
        type: 'getDataList',
        payload: response.data,
      });
    },
    *commotData({ payload }, { call, put, select }) {
      const data = yield select(state => state);
      const date = payload.date;
      yield put({
        type: 'editData',
        payload: { data, date },
      });
    },
  },

  reducers: {
    getDataList(state, { payload }) {
      return {
        ...state,
        customerName: payload.customerName,
        contactTel: payload.contactTel,
        isverify: payload.isverify,
      };
    },
    editData(state, { payload }) {
      const { date, data } = payload;
      console.log(date, 'up');
      const { isverify, customerName } = data.user;
      const { carAllList } = data.global;
      const hhh = {};
      const details = [];
      carAllList.map((item) => {
        hhh.internaltradeId = 1;
        hhh.unitName = item.unitName;
        hhh.amount = 100;
        hhh.freeSum = 20;
        hhh.state = 1;
        hhh.sum = 10;
        hhh.unitPrice = 10;
        details.push(hhh);
      });
      const obj = {
        cancelFlag: 0, // 是否取消订单,0未取消
        isweb: 0, // 0为手工数据
        isverify, // isverify为0未审核
        lumpSum: 0, // 总价
        state: 2, // 状态（下单 采购 入库 出库 收货）
        customerName, // 用户单位名称
        distributionDate: date, // 配送时间
        details,
      };
      return {
        ...state,
        orderData: obj,
      };
    },
  },
};
